/**
 * @file Login config
 */

import MD5 from 'md5';
import { sm3 } from 'sm-crypto';
import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
import sha256 from 'crypto-js/sha256';
import getQueryString from 'src/common/utils/getQueryString';
import {
  MODE_DEVELOPMENT,
  MODE_PRODUCTION,
  MODE_RELEASE,
  MODE_CURRENT,
  getLivePath
} from 'src/env';

const FORM_MAP = {
  [MODE_DEVELOPMENT]: {
    appKey: getQueryString('appkey') || '0b7f486bf7cf42b8ccc6b1d0791e5858',
    secretKey: getQueryString('secretkey') || 'e472b7eac4affb046498dbdaf0e7bc6b',
    roomID: getQueryString('roomid') || '878358504',
    account: getRandom(6),
    nickName: 'account_' + getRandom(6),
    open_id: 'okoZT6cdAJAeq8UhYfsyHZmQ61HU',
    email: '',
    isPC: true,
    signType: 0,
    isHorizontal: true,
    roomList: []
  },
  [MODE_RELEASE]: {
    appKey: getQueryString('appkey') || 'd3eaaff63bcb4d499420ff7684840495',
    secretKey: getQueryString('secretkey') || 'acaf01de7d9fdc65641b3671b295870f',
    roomID: getQueryString('roomid') || '370512514',
    account: getRandom(6),
    nickName: 'account_' + getRandom(6),
    open_id: '',
    email: '',
    signType: 0,
    roomList: []
  },
  [MODE_PRODUCTION]: {
    appKey: getQueryString('appkey') || 'd3eaaff63bcb4d499420ff7684840495',
    secretKey: getQueryString('secretkey') || 'acaf01de7d9fdc65641b3671b295870f',
    roomID: getQueryString('roomid') || '370512514',
    account: getRandom(6),
    nickName: 'account_' + getRandom(6),
    open_id: '',
    email: '',
    signType: 0,
    roomList: []
  }
};
// 线上-TODO

// 获取表单值
export const getFormValues = () => FORM_MAP[MODE_CURRENT];

// form表单字段
export const FIELD_LIST = [
  {
    label: 'APPKey',
    field: 'appKey',
    rule: { required: true, message: '请输入APPKey' },
    extra: `<a href="${getLivePath()}/v3/setting/dev" target="_blank">查看我的应用</a>`
  },
  {
    label: 'SecretKey',
    field: 'secretKey',
    rule: { required: true, message: '请输入APPKey' },
    extra: `<a href="https://saas-doc.vhall.com/docs/show/1282" target="_blank">JSSDK对接文档</a>`
  },
  {
    label: 'roomID (活动ID)',
    field: 'roomID',
    rule: { required: true, message: '请输入APPKey' },
    extra: `<a href="${getLivePath()}/v3/live/list" target="_blank">查看我的活动</a>`
  },
  {
    label: 'Account (三方用户唯一标识)',
    field: 'account',
    rule: { required: true, message: '请输入Account' }
  },
  {
    label: '昵称',
    field: 'nickName',
    rule: { required: true, message: '请输入昵称' }
  },
  {
    label: '微信open_id',
    field: 'open_id'
  },
  {
    label: '验签方式',
    field: 'signType',
    rule: { required: true, message: '请选择验签方式' }
  },
  {
    label: '观看限制',
    field: 'checkRestrict'
  }
];
// 验签方式
export const signOptions = [
  {
    id: 0,
    name: 'MD5'
  },
  {
    id: 1,
    name: 'RSA'
  },
  {
    id: 2,
    name: 'SHA256'
  },
  {
    id: 3,
    name: 'SM3'
  }
];
/**
 * object {Object} 参与签名参数  MD5方式，安全考虑请在服务端生成签名
 */
export const signEncrypt = (secretKey, type, object, publicKey) => {
  let str = secretKey;
  if (!object.email) {
    delete object.email;
  }
  if (!object.head) {
    delete object.head;
  }
  Object.keys(object)
    .sort()
    .forEach(item => {
      str += item + object[item];
    });
  str += secretKey;
  if (type === 0) {
    return MD5(str).toString();
  } else if (type === 2) {
    return sha256(str).toString();
  } else if (type === 3) {
    return sm3(str).toString();
  } else {
    // RSA
    return signRSA(object, publicKey);
  }
};
//RSA加密
export function signRSA(params, rsa_private_key) {
  let str = '';
  Object.keys(params)
    .sort()
    .forEach(item => {
      item = item.trim();
      if (Array.isArray(params[item])) {
        str += item + '';
      } else {
        let value = typeof params[item] === 'string' ? params[item].trim() : params[item];
        str += item + value;
      }
    });
  let md5str = MD5(str);
  console.log('MD5', md5str);
  let decrypt = new JSEncrypt();
  decrypt.setPrivateKey(rsa_private_key.trim()); //配置私钥
  let result = decrypt.sign(md5str, CryptoJS.MD5, 'md5');
  return encodeURI(result);
  //return btoa(encodeURI(result))//base64url_encode
}
/**
 * 生成随机数
 */
export function getRandom(m) {
  let str = '';
  while (m) {
    const n = Math.floor(Math.random() * 10);
    str += n;
    m--;
  }
  return str;
}
